﻿using System;
using System.Windows.Forms;

namespace Protobuf_Gui.Controls
{
    public partial class Main : Form, IMain
    {
        public Main()
        {
            InitializeComponent();
            KeyPreview = true;
            this.KeyDown += MainKeyDown;
            treeProject.AfterSelect += TreeProjectAfterSelect;
            btnDelete.Click += BtnDeleteClick;
            btnInsert.Click += BtnInsertClick;
        }

        void BtnInsertClick(object sender, EventArgs e)
        {
            OnDoOperation(Operation.Insert);
        }

        private const int Insert = 46;
        private const int Delete = 45;

        private void MainKeyDown(object sender, KeyEventArgs e)
        {
            int value = e.KeyValue;
            switch (value)
            {
                case Insert:
                    OnDoOperation(Operation.Insert);
                    break;
                case Delete:
                    OnDoOperation(Operation.Delete);
                    break;
            }
        }

        void BtnDeleteClick(object sender, EventArgs e)
        {
            OnDoOperation(Operation.Delete);    
        }

        public TreeNode SelectedNode
        {
            get
            {
                return treeProject.SelectedNode;
            }
        }

        public event Action<Operation> DoOperation;

        protected virtual void OnDoOperation(Operation operation)
        {
            Action<Operation> handler = DoOperation;
            if (handler != null) handler(operation);
        }

        void TreeProjectAfterSelect(object sender, TreeViewEventArgs e)
        {
            var action = e.Action;
            TreeNode node = e.Node;
            switch (action)
            {
                case TreeViewAction.ByKeyboard:
                case TreeViewAction.ByMouse:
                    OnNodeSelected(node);
                    break;
            }
        }
        
        public event Action<TreeNode> NodeSelected;

        protected virtual void OnNodeSelected(TreeNode obj)
        {
            Action<TreeNode> handler = NodeSelected;
            if (handler != null) handler(obj);
        }

        public event EventHandler BeginOpen
        {
            add { openProjectToolStripMenuItem.Click += value; }
            remove { openProjectToolStripMenuItem.Click -= value; }
        }

        public event EventHandler BeginSave
        {
            add { saveProjectToolStripMenuItem.Click += value; }
            remove { saveProjectToolStripMenuItem.Click -= value; }
        }

        public event EventHandler BeginExit
        {
            add { exitToolStripMenuItem.Click += value; }
            remove { exitToolStripMenuItem.Click -= value; }
        }

        public event EventHandler ShowAbout
        {
            add { aboutToolStripMenuItem.Click += value; }
            remove { aboutToolStripMenuItem.Click -= value; }
        }

        public event EventHandler BeginNew
        {
            add { newProjectToolStripMenuItem.Click += value; }
            remove { newProjectToolStripMenuItem.Click -= value; }
        }

        public Form AsForm { get { return this; }}
    }

    public enum Operation
    {
        Delete,
        Insert
    }
}